<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

class PimModelUserPim extends JModel {
	
	function __construct($where) {
		parent::__construct();
	}
	
	function count() {
		$query = "SELECT count(id) FROM #__users $where";
		$this->_db->setQuery($query);
		return $this->_db->loadResult();
	}
	
	function getData(&$total, &$dbrows , $order_by = 'id', $order_dir = 'DESC', $offset = 0, $limit = 30, $where=array()) {		
		$where = " WHERE ug.group_id IN(".ManagerHelper::ROLE_ADMIN.",".ManagerHelper::ROLE_REPORTER.",".ManagerHelper::ROLE_AGENCY.") ";

		if($limit !== 0)
			$limit = "LIMIT $offset, $limit";
		else
			$limit = '';
			
		$query = " SELECT u.*, ug.*, g.title as g_title  FROM #__users as u ";
		$query.= " LEFT JOIN #__user_usergroup_map as ug ON (u.id = ug.user_id) ";
		$query.= " LEFT JOIN #__usergroups as g ON (g.id = ug.group_id ) ";
		$query.= " $where GROUP BY u.id  ORDER BY $order_by $order_dir $limit";
		
		$this->_db->setQuery($query);
		$dbrows	= $this->_db->loadObjectList();

		$total = $this->count($where);
	}
	
	function getLoadObj($user_id) {	
		$where = " WHERE u.id = $user_id AND  ug.group_id IN(".ManagerHelper::ROLE_ADMIN.",".ManagerHelper::ROLE_REPORTER.",".ManagerHelper::ROLE_AGENCY.") ";
	
		$query = " SELECT u.*, ug.*, g.title as g_title  FROM #__users as u ";
		$query.= " LEFT JOIN #__user_usergroup_map as ug ON (u.id = ug.user_id) ";
		$query.= " LEFT JOIN #__usergroups as g ON (g.id = ug.group_id ) ";
		$query.= " $where GROUP BY u.id ";
				
		$this->_db->setQuery($query);
		$dbrows	= $this->_db->loadObject();
		return $dbrows;
	}	
}